package com.example.styservice.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.styinterface.interfaces.IAppleService;
import com.example.styinterface.utils.R;
import com.example.stymodel.model.Apple;

import com.example.styrepositories.irepoisitories.IAppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;


import javax.xml.ws.soap.Addressing;
import java.util.List;
@Component
@Service
public class AppleServiceImpl implements IAppleService {

    @Autowired
    private IAppleRepository appleRepository;
//    //todo ?导入不了
//    //    private final IAppleRepository appleRepository;
////    public AppleServiceImpl( IAppleRepository appleRepository) {
////        this.appleRepository = appleRepository;
////
////    }

    @Override
    public R insert(Apple apple) {
        appleRepository.insert(apple);
        return R.success(200,"插入成功");
    }

    @Override
    public R update(Apple apple) {
        Apple apple1=appleRepository.selectOne(apple.getId());
        if(apple1==null){
            return R.success(200,"未找到信息");
        }
        appleRepository.update(apple);
        return R.success(200,"更新成功");

    }

    @Override
    public R delete(Integer id) {
        Apple apple1=appleRepository.selectOne(id);
        if(apple1==null){
            return R.success(200,"未找到信息");
        }
        appleRepository.delete(id);
        return R.success(200,"删除成功");
    }

    @Override
    public R selectAll(){
        List<Apple> appleList=appleRepository.selectAll();
        return R.success(appleList,200,"查找成功");

    }
    @Override
    public R selectOne(Integer id){
        Apple apple=appleRepository.selectOne(id);
        if(apple==null){
            return R.success(200,"未查询到有关信息");
        }
        return R.success(apple,200,"查找成功");
    }
}
